package com.lenovo.manufacture;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RawMaterial extends AppCompatActivity {
    private static final String TAG = "RawMaterial";
    private TextView tvMoney;
    private EditText etPosition;
    private EditText etId;
    private Button btReturn;
    private Button btBuy;
    private final Retrofit retrofit=new Retrofit.Builder()
            .baseUrl("http://hh1.me:5001/Interface/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    private List<StudentFactoryData.DataBean> dataBeans=new ArrayList<>();
    private List<GetSearchData.DataBean> dataBMaterial=new ArrayList<>();
    private List<GetAddData> data=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_raw_material);
        initView();
        getMoney(1);
    }

    private void getMoney(int id) {
        Service service=retrofit.create(Service.class);
        Call<StudentFactoryData> isFactory=service.isFactory(id);
        isFactory.enqueue(new Callback<StudentFactoryData>() {
            @Override
            public void onResponse(@NonNull Call<StudentFactoryData> call,@NonNull Response<StudentFactoryData> response) {
                if (response.body() != null) {
                    dataBeans = response.body().getData();
                    tvMoney.setText("工厂资金" + dataBeans.get(0).getGold());
                }else {
                    Log.d(TAG, "onResponse: response body is null");
                }
                //Log.d("TAG", "onResponse: "+response.body().getData().get(0).getGold());
            }
            @Override
            public void onFailure(@NonNull Call<StudentFactoryData> call,@NonNull Throwable t) {
                Log.d(TAG, "onFailure: " + t.getMessage());
            }
        });
    }

    //位置
    private void getMaterial(int id){
        Service service=retrofit.create(Service.class);
        Call<MaterialData> ismaterial=service.isMaterial();
        ismaterial.enqueue(new Callback<MaterialData>() {
            @Override
            public void onResponse(@NonNull Call<MaterialData> call,@NonNull Response<MaterialData> response) {
                List<MaterialData.DataBean> datab=response.body().getData();
                for (MaterialData.DataBean dataBean : datab){
                    if (dataBean.getMaterialId()==id){
                        if (etPosition.getText().length()>0){
                            showChange(Integer.parseInt(etPosition.getText().toString()), dataBean.getSupplyId());
                        }else {
                            show("请填写要购买的位置");
                        }
                        return;
                    }
                }
                show("无");
            }
            @Override
            public void onFailure(@NonNull Call<MaterialData> call,@NonNull Throwable t) {
                Log.d(TAG, "onFailure: " + t.getMessage());
            }
        });
    }

    private void getId(int po, int number, int supplyId){
        Service service=retrofit.create(Service.class);
        Call<GetSearchData> getSearch=service.getSearch(1);
        getSearch.enqueue(new Callback<GetSearchData>() {
            @Override
            public void onResponse(Call<GetSearchData> call, Response<GetSearchData> response) {
                List<GetSearchData.DataBean> dataBeans=response.body().getData();
                Log.d(TAG, "onResponse: "+po);
                for (GetSearchData.DataBean dataBean:dataBeans){
                    //进货的条件
                    // 不进货的条件
                    if(dataBean.getStatus()==1||dataBean.getStatus()==2){
                        getAdd(dataBean.getLineId(),number,supplyId );
                    }else if(dataBean.getStatus()==0) {
                        show("生产线"+dataBean.getLineId()+"建设中");
                    }else if(dataBean.getStatus()==3) {
                        show("生产线"+dataBean.getLineId()+"库存已满");
                    }
                }
            }
            @Override
            public void onFailure(Call<GetSearchData> call, Throwable t) {
                Log.d(TAG, "onFailure: " + t.getMessage());
            }
        });
    }

    //购买
    private void getAdd(int userLineId, int number, int supplyListId){
        Service service=retrofit.create(Service.class);
        Call<GetAddData> getAdd=service.getAdd(userLineId,number,supplyListId);
        getAdd.enqueue(new Callback<GetAddData>() {
            @Override
            public void onResponse(Call<GetAddData> call, Response<GetAddData> response) {
                Toast.makeText(RawMaterial.this, "已购买", Toast.LENGTH_SHORT).show();
                if (response.body().getMessage().equals("成功")) {
                    show("生产线"+userLineId+"已购买原材料");
                } else {
                    show("生产线"+userLineId+"购买原材料失败");
                }
                getMoney(1);
            }

            @Override
            public void onFailure(Call<GetAddData> call, Throwable t) {

            }
        });
    }

    private void showChange(int pos, int supplyId){
        View view= LayoutInflater.from(RawMaterial.this).inflate(R.layout.num_row,null);
        EditText etnum=view.findViewById(R.id.et_num);
        Button btcancel=view.findViewById(R.id.btn_cancel);
        Button btconfirm=view.findViewById(R.id.btn_confirm);
        AlertDialog alertDialog = new AlertDialog.Builder(RawMaterial.this)
                .setTitle("请输入购买数量")
                .setView(view)
                .create();
        alertDialog.setCancelable(false);
        btconfirm.setOnClickListener(v -> {
            Log.d(TAG, "getId: --------1");
            if (etnum.getText().length()>0){
                Log.d(TAG, "getId: --------2");
                getId(pos,Integer.parseInt(etnum.getText().toString()),supplyId);
                alertDialog.cancel();
            }else {
                Log.d(TAG, "getId: --------3");
                Toast.makeText(this, "！！！", Toast.LENGTH_SHORT).show();
            }
        });
        btcancel.setOnClickListener(v -> alertDialog.cancel());
        alertDialog.show();
    }

    private void initView() {
        etPosition=findViewById(R.id.et_position);
        etId =findViewById(R.id.et_id);
        tvMoney =findViewById(R.id.tv_money);
        btBuy =findViewById(R.id.bt_buy);
        btReturn=findViewById(R.id.bt_return);
        btReturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        btBuy.setOnClickListener((v) -> {
            if (etId.getText().length() > 0) {
                getMaterial(Integer.parseInt(etId.getText().toString()));
            } else {
                show("请填写要购买的材料ID");
            }
        });
        getMoney(1);
    }

    private void show(String message) {
        AlertDialog.Builder builder = new AlertDialog.Builder(RawMaterial.this);
        builder.setTitle("提示");
        builder.setMessage(message);
        builder.setPositiveButton("确定", (dialog, which) -> {
        });
        builder.show();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }
}